package com.example.interviewrecorder.components

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.WindowManager
import com.example.interviewrecorder.R
import com.example.interviewrecorder.databinding.ChangePasswordDialogLayoutBinding

class ChangePasswordDialog(
    context: Context,
    var onCancel: (() -> Unit)? = null,
    var onConfirm: ((oldPassword: String, newPassword1: String, newPassword2: String, dlg: ChangePasswordDialog) -> Unit)? = null,
) : Dialog(context) {
    private val binding: ChangePasswordDialogLayoutBinding =
        ChangePasswordDialogLayoutBinding.inflate(
            LayoutInflater.from(context)
        )

    init {
        setContentView(binding.root)
        setCancelable(false)
        binding.cancelBtn.setOnClickListener {
            onCancel?.invoke()
            dismiss()
        }
        binding.confirmBtn.setOnClickListener {
            val oldPassword = binding.oldPasswordEt.text.toString().trim()
            val newPassword1 = binding.newPasswordEt1.text.toString().trim()
            val newPassword2 = binding.newPasswordEt2.text.toString().trim()
            onConfirm?.invoke(oldPassword, newPassword1, newPassword2, this)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //给弹框设置圆角背景
        window?.setBackgroundDrawableResource(R.drawable.lyy_dialog_bg)

        //设置弹框的宽度为屏幕的90%
        window?.setLayout((getScreenWidth() * 0.9).toInt(), WindowManager.LayoutParams.WRAP_CONTENT)
    }

    private fun getScreenWidth(): Int {
        return context.resources.displayMetrics.widthPixels
    }
}